package pdf;

import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.DocumentException;

/**
 * @author yirong610
 *
 * @modify zgj
 * 根据pdf模板生成pdf，不够灵活，暂时未使用
 */
public class ITexts {

    public static void main(String[] args) throws Exception {
        test();
    }

    public static void test() throws IOException, DocumentException, com.itextpdf.text.DocumentException {
        String fileName = "D:/workspace/projectBackUps/work/常州一院\\PdfTemplate/testTemplate.pdf"; // pdf模板   testTemplate
//    	 String fileName = "D:/workspace/projectBackUps/work/常州一院\\PdfTemplate/111.pdf"; // pdf模板   testTemplate
        PdfReader reader = new PdfReader(fileName);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        /* 将要生成的目标PDF文件名称 */
        PdfStamper ps = new PdfStamper(reader, bos);
        PdfContentByte under = ps.getUnderContent(1);
        /* 使用中文字体 */
        //BaseFont bf = BaseFont.createFont("c://windows//fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        BaseFont bf = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font FontChinese = new Font(bf, 2, Font.NORMAL);
        ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
        fontList.add(bf);
        /* 取出报表模板中的所有字段 */
        AcroFields fields = ps.getAcroFields();
        fields.setSubstitutionFonts(fontList);
        fillData(fields, data());
        /* 必须要调用这个，否则文档不会生成的 */
        ps.setFormFlattening(true);
        ps.close();
        OutputStream fos = new FileOutputStream("D:/workspace/projectBackUps/work/常州一院/PdfTemplate/1.pdf");
        fos.write(bos.toByteArray());
        fos.flush();
        fos.close();
        bos.close();
    }

    public static void fillData(AcroFields fields, Map<String, String> data)
            throws IOException, DocumentException, com.itextpdf.text.DocumentException {
        java.util.Iterator<String> it = fields.getFields().keySet().iterator();
        while (it.hasNext()) {
            String name = it.next().toString();
            String value = data.get(name) != null ? data.get(name).toString() : null;
            System.out.println("name = " + name + " value = " + value);
            fields.setField(name, value);
        }
    }

    public static Map<String, String> data() {
        Map<String, String> data = new HashMap<String, String>();
        data.put("Name", "test国光");
        data.put("bianhao", "xx第10000001号");
        data.put("number", "国test光");
        data.put("aihao", "打篮球、看电影");
        data.put("txt", "国test光国test光国test光国test光国test光国test光国test光国test光" +
            "国test光国test光国test光国test光国test光国test光国test光国test光国test光国test" +
            "光国test光国test国test光国test光国test光国test光国test光国test光国test光国te" +
            "st光国test光国test国test光国test光国test光国test光国test光国test光国test光国" +
            "test光国test光国test国test光国test光国test光国test光国test光国test光国test光" +
            "国test光国test光国test国test光国test光国test光国test光国test光国test光国test" +
            "光国test光国test光国test国test光国test光国test光国test光国test光国test光国tes" +
            "t光国test光国test光国test国test光国test光国test光国test光国test光国test光国tes" +
            "t光国test光国test光国test国test光国test光国test光国test光国test光国test光国test" +
            "光国test光国test光国test213");
        data.put("DateTime", "2022-04-16");
        data.put("a", "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
            + "12345678901234567890123456789012345678901234567890123456789012345678901234567890");

//        data.put("a", "国test光国test光国test光国test光国test光国test光国test光国test光国test光国test光国test光国test光国test光国test光国test光国test光国test光国test光国test光国test国test光国test光国test光国test光国test光国test光国test光国test光国test光国test国test光国test光国test光国test光国test光国test光国test光国test光国test光国test国test光国test光国test光国test光国test光国test光国test光国test光国test光国test国test光国test光国test光国test光国test光国test光国test光国test光国test光国test国test光国test光国test光国test光国test光国test光国test光国test光国test光国test国test光国test光国test光国test光国test光国test光国test光国test光国test光国test国test光国test光国test光国test光国test光国test光国test光国test光国test光国test");
//        data.put("b", "13242343242342332132423432423423321324234324234233213242343242342332"
//        		+ "13242343242342332132423432423423321324234324234233213242343242342332"
//        		+ "13242343242342332132423432423423321324234324234233213242343242342332"
//        		+ "13242343242342332132423432423423321324234324234233213242343242342332"
//        		+ "13242343242342332132423432423423321324234324234233213242343242342332"
//        		+ "13242343242342332132423432423423321324234324234233213242343242342332"
//        		+ "13242343242342332132423432423423321324234324234233213242343242342332");
//        data.put("c", "2");
//        data.put("d", "3");
//        data.put("e", "4");
//        data.put("f", "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
//        		+ "12345678901234567890123456789012345678901234567890123456789012345678901234567890");
        return data;
    }
}
